package com.my_musc_service.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my_musc_service.dao.CommentDao;
import com.my_musc_service.entity.Comment;
import com.my_musc_service.entity.CommentVO;
import com.my_musc_service.entity.TopComment;
import com.my_musc_service.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author A.Dun
 * @since 2021-08-11 12:13:04
 */
@Deprecated
@Slf4j
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {
    @Resource
    private CommentDao commentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Comment queryById(Integer id) {
        return this.commentDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Comment> queryAllByLimit(int offset, int limit) {
        return this.commentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public Comment insert(Comment comment) {
        this.commentDao.insert(comment);
        return comment;
    }

    /**
     * 修改数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public Comment update(Comment comment) {
        this.commentDao.update(comment);
        return this.queryById(comment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.commentDao.deleteById(id) > 0;
    }

    @Override
    public TopComment getCommentVOByCommentId(Integer id, Integer songListId, Integer songId) {
        TopComment topComment = new TopComment();

        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if (id != null) {
            queryWrapper.eq("id", id);
        }
        if (songListId != null) {
            queryWrapper.eq("song_list_id", songListId);
        }
        if (songId != null) {
            queryWrapper.eq("song_id", songId);
        }
        queryWrapper.orderByAsc("id");
        List<Comment> commentAllList = baseMapper.selectList(queryWrapper);

        //顶级评论
        List<Comment> topCommentList = commentAllList.stream().filter(comment -> 0 == comment.getParentId()).collect(Collectors.toList());
        //其他级评论
        List<Comment> nextCommentList = commentAllList.stream().filter(comment -> 0 != comment.getParentId()).collect(Collectors.toList());
        //k-v===>parentId-Comment
        Map<Integer, List<Comment>> parentCommentGroup = commentAllList.stream().collect(Collectors.groupingBy(Comment::getParentId));

        List<CommentVO> topChildCommentList = new ArrayList<>();
        for (Comment comment : topCommentList) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            topChildCommentList.add(commentVO);
            getAllChildCommentList(commentVO, parentCommentGroup);
        }
        topComment.setChildCommentList(topChildCommentList);
        System.out.println(JSON.toJSONString(topChildCommentList));
        return topComment;
    }




    private void getAllChildCommentList(CommentVO commentVO, Map<Integer, List<Comment>> parentCommentGroup) {
        Integer id = commentVO.getId();
        //获取当前评论的子一级评论
        List<Comment> childCommentList = parentCommentGroup.get(id);
        if (!CollectionUtils.isEmpty(childCommentList)) {
            List<CommentVO> childCommentVOList = childCommentList.stream().map(comment1 -> {
                CommentVO commentVO1 = new CommentVO();
                BeanUtils.copyProperties(comment1, commentVO1);
                return commentVO1;
            }).collect(Collectors.toList());
            commentVO.setChildCommentList(childCommentVOList);
            for (CommentVO vo : childCommentVOList) {
                getAllChildCommentList(vo, parentCommentGroup);
            }
        }

    }
}
